package example.cloudsitemanagement8002.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import example.cloudsitemanagement8002.dto.SiteInfoQueryDTO;
import example.cloudsitemanagement8002.entity.SiteInfo;
import example.cloudsitemanagement8002.mapper.SiteInfoMapper;
import example.cloudsitemanagement8002.service.SiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SiteInfoServiceImpl implements SiteInfoService {

    @Autowired
    private SiteInfoMapper siteInfoMapper;

    @Override
    public PageInfo<SiteInfo> listSites(SiteInfoQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<SiteInfo> siteInfoList = siteInfoMapper.selectSiteInfoList(queryDTO.getName());
        return new PageInfo<>(siteInfoList);
    }

    @Override
    public PageInfo<SiteInfo> listSiteOptions(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SiteInfo> siteInfoList = siteInfoMapper.selectSiteInfoList(null); // Assuming null fetches all
        return new PageInfo<>(siteInfoList);
    }

    @Override
    public SiteInfo getSiteById(Long id) {
        return siteInfoMapper.selectSiteInfoById(id);
    }

    @Override
    public boolean addSite(SiteInfo siteInfo) {
        siteInfo.setCreateTime(LocalDateTime.now());
        siteInfo.setUpdateTime(LocalDateTime.now());
        // siteInfo.setCreateBy("admin"); // Or get from security context
        // siteInfo.setUpdateBy("admin"); // Or get from security context
        int count = siteInfoMapper.insertSiteInfo(siteInfo);
        return count > 0;
    }

    @Override
    public boolean updateSite(SiteInfo siteInfo) {
        siteInfo.setUpdateTime(LocalDateTime.now());
        // siteInfo.setUpdateBy("admin"); // Or get from security context
        int count = siteInfoMapper.updateSiteInfoById(siteInfo);
        return count > 0;
    }

    @Override
    public boolean deleteSite(Long id) {
        int count = siteInfoMapper.deleteSiteInfoById(id);
        return count > 0;
    }
}
